<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
$lotto_id = intval($_GET['id']);
$conf = new Config();
$startbet = $conf->get("startbet");
$stopbet = $conf->get("waitbet");
$prot_start = $conf->get("protect_start");
if (!isset($_SESSION['bsaystartmod']))
{
    $_SESSION['bsaystartmod'] = 0;
}
$saystartmod = $_SESSION['bsaystartmod'];
if (!isset($_SESSION['bsaystopmod']))
{
    $_SESSION['bsaystopmod'] = 0;
}
$saystopmod = $_SESSION['bsaystopmod'];
if ($stopbet <= date("Hi") && !$saystopmod)
{
    $saystopmod = 1;
    $_SESSION['bsaystopmod'] = $saystopmod;
    echo "-1";
    exit();
}
$stopmode = $prot_start == 0 ? 1 : $saystopmod;
if ($saystopmod == 0 && $prot_start != $saystartmod)
{
    $saystartmod = $prot_start;
    $_SESSION['bsaystartmod'] = $saystartmod;
    $stopmode = $saystartmod;
    echo "-1";
    exit();
}
$split = 1;
$percent = 1;
$betids = array(21, 22,37, 38);
$total_win = array();
$ary = array();
foreach ($betids as $betid)
{
    $total_win[$betid] = 0;
    $ary[$betid] = array();
}
$i = 1;
while ($i < 13)
{
    $ary[21][$i] = array("rate" => 0, "amount" => 0, "count" => 0, "lost" => 0);
    $ary[37][$i] = array("rate" => 0, "amount" => 0, "count" => 0, "lost" => 0);
    ++$i;
}
$i = 1;
while ($i < 11)
{
    $ary[22][$i] = array("rate" => 0, "amount" => 0, "count" => 0, "lost" => 0);
    $ary[38][$i] = array("rate" => 0, "amount" => 0, "count" => 0, "lost" => 0);
    ++$i;
}
$sql = "select * from bet where lotto_id=" . $lotto_id . " and bettype in (21,22,37,38)";
$theacl = $curruser['acl'];
$userid = $curruser['id'];
$userlist = GetUserList($userid);
if ($userlist == "")
{
    exit("Not Users!");
    exit();
}
$sql .= " and user_id in (" . $userlist . ") and rame in (0,9) order by bettype";
$rs = getdb($sql);
while (!$rs->eof)
{
    $percent = 1;
    $backs = 100;
    if ($theacl == 5)
    {
        $backs = $rs->f['back4'];
        $percent = $rs->f['percent4'];
    }
    else if ($theacl == 4)
    {
        $backs = $rs->f['back3'];
        $percent = $rs->f['percent3'];
    }
    else if ($theacl == 3)
    {
        $backs = $rs->f['back2'];
        $percent = $rs->f['percent2'];
    }
    else
    {
        $backs = $rs->f['back1'];
        $percent = $rs->f['mpercent'];
    }
    if (0 < $percent)
    {
        $percent /= 100;
    }
    $tmp = $percent * $rs->f['amount'];
    $num = intval($rs->f['numbers']);
    $ary[$rs->f['bettype']][$num]['amount'] += $tmp;
    ++$ary[$rs->f['bettype']][$num]['count'];
    $ary[$rs->f['bettype']][$num]['lost'] += $tmp * $rs->f['rate'];
    $total_win[$rs->f['bettype']] += $tmp * ( $backs / 100 );
    $rs->movenext();
}
$rs->close();
if (2 < $theacl)
{
    $sql = "select * from bet2 where lotto_id=" . $lotto_id . " and uperid" . $theacl . "=" . $userid . " and bettype in (21,22,37,38) and rame in (0,9) order by bettype";
    $rs = getdb($sql);
    while (!$rs->eof)
    {
        $percent = 1;
        $backs = 100;
        if ($theacl == 5)
        {
            $backs = $rs->f['back5'];
            $percent = $rs->f['percent5'];
        }
        else if ($theacl == 4)
        {
            $backs = $rs->f['back4'];
            $percent = $rs->f['percent4'];
        }
        else if ($theacl == 3)
        {
            $backs = $rs->f['back3'];
            $percent = $rs->f['percent3'];
        }
        if (0 < $percent)
        {
            $percent /= 100;
        }
        if ($rs->f['prev_id'] == 0)
        {
            $percent = 0;
        }
        $tmp = $percent * $rs->f['amount'];
        $num = intval($rs->f['numbers']);
        $ary[$rs->f['bettype']][$num]['amount'] += $tmp;
        ++$ary[$rs->f['bettype']][$num]['count'];
        $ary[$rs->f['bettype']][$num]['lost'] += $tmp * ( $rs->f['rate'] + $rs->f['rate5'] );
        $total_win[$rs->f['bettype']] += $tmp * ( $backs / 100 );
        $rs->movenext();
    }
    $rs->close();
}
$sql = "select * from bet2 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and bettype in (21,22,37,38) and rame in (0,9) order by bettype";
$rs = getdb($sql);
while (!$rs->eof)
{
    $backs = 100;
    if ($theacl == 5)
    {
        $backs = $rs->f['back5'];
    }
    else if ($theacl == 4)
    {
        $backs = $rs->f['back4'];
    }
    else if ($theacl == 3)
    {
        $backs = $rs->f['back3'];
    }
    else if ($theacl == 2)
    {
        $backs = $rs->f['back2'];
    }
    $tmp = $rs->f['amount'];
    $num = intval($rs->f['numbers']);
    $ary[$rs->f['bettype']][$num]['amount'] -= $tmp;
    $ary[$rs->f['bettype']][$num]['lost'] -= $tmp * $rs->f['rate'];
    $total_win[$rs->f['bettype']] -= $tmp * ( $backs / 100 );
    $rs->movenext();
}
$rs->close();
$rs = getdb("select * from rate1a where lotto_id=" . $lotto_id);
if ($rs->eof)
{
    exit("Error ID!");
}
$rs1 = get_srates_data($userid, $lotto_id, 21);
if ($rs1->eof)
{
    exit("Error ID!");
}
if ($theacl == 5)
{
    $i = 1;
    while ($i < 13)
    {
        $ary[21][$i]['rate'] = $rs->f["r21_" . $i] - 0;
        ++$i;
    }
    $i = 1;
    while ($i < 11)
    {
        $ary[22][$i]['rate'] = $rs->f["r22_" . $i] - 0;
        ++$i;
    }
}
else
{
    $i = 1;
    while ($i < 13)
    {
        $ary[21][$i]['rate'] = $rs->f["r21_" . $i] - $rs1->f["r21_" . $i] - 0;
        ++$i;
    }
    $i = 1;
    while ($i < 11)
    {
        $ary[22][$i]['rate'] = $rs->f["r22_" . $i] - $rs1->f["r22_" . $i] - 0;
        ++$i;
    }
}
$rs->close();
$j = 1;
while ($j < 13)
{
    $ary[21][$j]['amount'] += $ary[37][$j]['amount'];
    $ary[21][$j]['count'] += $ary[37][$j]['count'];
    $ary[21][$j]['lost'] += $ary[37][$j]['lost'];
    ++$j;
}
$j = 1;
while ($j < 11)
{
    $ary[22][$j]['amount'] += $ary[38][$j]['amount'];
    $ary[22][$j]['count'] += $ary[38][$j]['count'];
    $ary[22][$j]['lost'] += $ary[38][$j]['lost'];
    ++$j;
}
$total_win[21] += $total_win[37];
$total_win[22] += $total_win[38];
echo $total_win[21];
$i = 1;
while ($i < 13)
{
    echo "," . $ary[21][$i]['rate'] . "@" . $ary[21][$i]['count'] . "@" . $ary[21][$i]['amount'];
    echo "@" . $ary[21][$i]['lost'] . "@" . $stopmode;
    ++$i;
}
echo "\n";
echo $total_win[22];
$i = 1;
while ($i < 11)
{
    echo "," . $ary[22][$i]['rate'] . "@" . $ary[22][$i]['count'] . "@" . $ary[22][$i]['amount'];
    echo "@" . $ary[22][$i]['lost'] . "@" . $stopmode;
    ++$i;
}
echo "\n";
include( "class_6h/class.menu.php" );
$fieldArr = array(
    "lotto_id" => $lotto_id,
    "split_id" => 1
);
$onlineMenu = new menu_6h(12, $fieldArr);
echo $onlineMenu->getMenuAmu();
echo "\n";
exit();